package cn.fjy.android.phone.receiver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import cn.fjy.android.phone.worker.TalkTimeRemind;
import cn.fjy.android.res.Constant;
import cn.fjy.android.utils.APPSession;
import cn.fjy.android.utils.GRPropertiesUtil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;

/**
 * 呼出电话动作监听
 * 
 * @author GrayF
 * @2011-3-24
 */
public class PhoneReceiver extends BroadcastReceiver {

	private String tag = "GR_PhoneReceiver";

	// 通话时间提醒服务
	private TalkTimeRemind talkTimeRemind;

	// 震动器
	private Vibrator vibrator;

	// 是否开启接通震动
	public static boolean isEnable = false;
	
	// 接通震动时间
	public static int vibratorTime = 100;
	
	private static boolean isInit = false;

	public PhoneReceiver() {
		initConfig();
		isInit = true;
	}

	
	public void initConfig() {
		GRPropertiesUtil prop = new GRPropertiesUtil(Constant.CONFIG_FILE_NAME);
		//  接通震动开关
		String tempValue = prop.getValue(Constant.PROP_KEY_CALL_ON_VIBRATOR);
		if (tempValue != null && !"".equals(tempValue.trim())) {
			if ("0".equals(tempValue)) {
				isEnable = true;
			} else {
				isEnable = false;
			}
		}

		// 通话时间提示方式
		tempValue = prop.getValue(Constant.PROP_KEY_TALKTIME_REMIND);
			if (tempValue != null && !"".equals(tempValue.trim())) {
				if ("0".equals(tempValue)) {
					TalkTimeRemind.isEnable = true;
				} else {
					TalkTimeRemind.isEnable = false;
				}
		}

		// 初始化提示震动时间
		tempValue = prop.getValue(Constant.PROP_VALUE_CALL_ON_VIBRATOR);
		if (tempValue != null && !"".equals(tempValue.trim())) {
			try {
				vibratorTime = Integer.valueOf(tempValue);
			} catch (Exception e) {
				vibratorTime = 100;
			}
		}
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		if(!isInit){
			initConfig();
			isInit = true;
		}
		if (!isEnable && !TalkTimeRemind.isEnable) {
			// 如果未开启接通震动和通话时间提示则不进入电话状态监测
			return;
		}
		vibrator = (Vibrator) context
				.getSystemService(Context.VIBRATOR_SERVICE);
		Log.i("fjy_in", "calling");
		
		talkTimeRemind = (TalkTimeRemind)APPSession.getValue(Constant.SESSION_KEY_TAKLTIMEREMID);
		if(talkTimeRemind==null){
			talkTimeRemind = new TalkTimeRemind(vibrator);
			APPSession.setValue(Constant.SESSION_KEY_TAKLTIMEREMID, talkTimeRemind);
		}
		new checkPhoneStatus().start();
	}

	class checkPhoneStatus extends Thread {

		@Override
		public void run() {
			Process process;
			InputStream inputstream;
			BufferedReader bufferedreader;
			try {
				process = Runtime.getRuntime().exec("logcat -s CallCard:D");
				inputstream = process.getInputStream();
				InputStreamReader inputstreamreader = new InputStreamReader(
						inputstream);
				bufferedreader = new BufferedReader(inputstreamreader);
				String str = "";
				while (str != null) {
					str = bufferedreader.readLine();
					// 电话接通
					if (str.contains("call.state: ACTIVE")) {
						if (isEnable) {
							vibrator.vibrate(vibratorTime);
						}
						if (TalkTimeRemind.isEnable&&!talkTimeRemind.isActive()) {
							new Thread(talkTimeRemind).start();
						}
						break;
					}
					if (str.contains("call.state: DISCONNECTED")) {
						break;
					}
				}
			} catch (IOException e) {
				Log.e(tag, e.getMessage());
			}
		}

	}

}
